#include <stdint.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include <poll.h>

#define DELAY_CMD_WRITE   _IOW('A',0,unsigned int)
#define DELAY_CMD_READ   _IOW('A',1,unsigned int)

static void* write01(void *ptr) {
	int fd = open("/dev/t01", O_RDWR, O_NONBLOCK);
	if (fd < 0) {
		perror("open");
		printf("error open\n");
		return NULL;
	}
		printf("write start \n");
		ioctl(fd, DELAY_CMD_WRITE, 100);
		printf("write end \n");
		sleep(2);
	close(fd);
	return NULL;
}

static void* read01(void *ptr) {
	int fd = open("/dev/t01", O_RDWR, O_NONBLOCK);
	if (fd < 0) {
		perror("open");
		printf("error open\n");
		return NULL;
	}
		printf("read start \n");
		ioctl(fd, DELAY_CMD_READ, 100);
		printf("read end \n");
		sleep(1);
	close(fd);
	return NULL;
}

/**
 *  one  write,
 *  two  read
 */
int main() {
	pthread_t thread, thread2, thread3;
	pthread_create(&thread, NULL, write01, NULL);
	usleep(10*1000);
	pthread_create(&thread2, NULL, read01, NULL);
	pthread_join(thread, NULL);
	pthread_join(thread2, NULL);
	printf("main exit\n");
	return EXIT_SUCCESS;
}

